﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Entity.Model;
using OneMile.Common;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MS0003", Name = "商家获取订单详情")]
    public class SGetOrderDetails : BaseAPI,IProcessMessage
    {
        private DALSOrder dalShopOrder;
        protected DALSOrder DalShopOrder
        {
            get
            {
                if (dalShopOrder == null)
                {
                    dalShopOrder = DALFactory.getInstance().CreateSOrderDAL();
                }
                return dalShopOrder;
            }
        }

        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string OrderId = reqMsg.Body.GetFieldValue("OrderId");
                if (String.IsNullOrEmpty(OrderId))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                Guid guid = new Guid(OrderId);
                //开始合并相同的项
                List<EOrderDetails> list = (List<EOrderDetails>)DalShopOrder.GetOrderDetails(guid);
                List<EOrderDetails> tempList= list.Distinct(new MyComparer()).ToList<EOrderDetails>();
                foreach (var o in tempList)
                 {
                   foreach (var p in list)
                    {
                        if (p.CommodityId == o.CommodityId && p.AttributeDetailsName != o.AttributeDetailsName)
                      {
                          o.AttributeDetailsName += "," + p.AttributeDetailsName;
                      }
                    }
                 }
                //结束

                if (tempList == null || tempList.Count == 0)
                {
                    rspMsg.Head.RspCode = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;
                    return rspMsg;
                }
                var fl = rspMsg.Body.AddFieldList("Order");
                for (int i = 0; i < tempList.Count; i++)
                {

                    var fll = fl.AddFieldList("OrderDetail");
                    fll.AddField("CommodityName", tempList[i].CommodityName);
                    fll.AddField("CommodityId", tempList[i].CommodityId.ToString());
                    fll.AddField("Amount", tempList[i].Amount.ToString());
                    fll.AddField("UnitPrice", tempList[i].UnitPrice.ToString("F2"));
                    fll.AddField("TotalPrice", tempList[i].TotalPrice.ToString("F2"));
                    fll.AddField("Discount", tempList[i].Discount.ToString("F2"));
                    fll.AddField("DiscountPrice", tempList[i].DiscountPrice.ToString("F2"));
                    fll.AddField("ActualTotalPrice", tempList[i].ActualTotalPrice.ToString("F2"));
                    fll.AddField("CommodityAccessoriesName", tempList[i].CommodityAccessoriesName);
                    fll.AddField("CommodityAccessoriesPrice", tempList[i].CommodityAccessoriesPrice.ToString());
                    fll.AddField("AttributeDetailsName", tempList[i].AttributeDetailsName);
                    fll.AddField("SpecificationName", tempList[i].SpecificationName);
                }
                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("ShopGetOrderDetails" + e.Message);
                throw e;
            }
        }

        #endregion
        public class MyComparer : IEqualityComparer<EOrderDetails>
        {
            public bool Equals(EOrderDetails x, EOrderDetails y)
            {
                if (x == null || y == null)
                {
                  return false;
                }
                return x.CommodityId == y.CommodityId;
            }

            public int GetHashCode(EOrderDetails obj)
            {
                return obj.ToString().GetHashCode();
            }
        }

    }
}